package com.example.project_station.task;


import com.alibaba.fastjson.JSONObject;
import com.example.project_station.pojo.*;
import com.example.project_station.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class StationTask {

    @Autowired
    private WebSocketServer webSocketServer;

    //每分钟执行一次
    @Scheduled(cron = "0 * * * * ?")
    public void stationScheduled() throws IOException {
        String maxTxt="E:\\大数据集群\\max.txt";
        BufferedReader reader=new BufferedReader(new FileReader(maxTxt));
        String max = reader.readLine();
        reader.close();
        String newPath="E:\\大数据集群\\station_output_All\\output_station_"+max+"\\part-r-00000";
        FileInputStream in = new FileInputStream(new File(newPath));

        InputStreamReader inReader = new InputStreamReader(in, "UTF-8");

        BufferedReader bufReader = new BufferedReader(inReader);

        String line = null;

        int i = 1;
        //1
        List<Long> lineA=new ArrayList<>();
        List<Long> lineB=new ArrayList<>();
        List<Long> lineC=new ArrayList<>();
        Map<Integer,Long> lineAmap=new HashMap<>();
        Map<Integer,Long> lineBmap=new HashMap<>();
        Map<Integer,Long> lineCmap=new HashMap<>();
        //2
        List<String> lineIdTwo=new ArrayList<>();
        List<Long> number=new ArrayList<>();
        //3
        List<String>  lineIdThree=new ArrayList<>();
        List<Long>  peopleNumber=new ArrayList<>();
        //4
        List<String> userID=new ArrayList<>();
        List<Integer>  numberTime=new ArrayList<>();
        //5
        List<String> payType=new ArrayList<>();
        List<Long> payNumber=new ArrayList<>();

        while((line = bufReader.readLine()) != null) {
            //读取数据中的key和vaule
            String key=line.split("\\t")[0];
            String value=line.split("\\t")[1];
            //将key 以-进行拆分
            String[] split_list = key.split("-");
            if (split_list[0].equals("1")){
                //为第一维度
                if (split_list[2].equals("A")){
                    lineAmap.put(Integer.parseInt(split_list[1]),Long.parseLong(value));
                }else if (split_list[2].equals("B")){
                    lineBmap.put(Integer.parseInt(split_list[1]),Long.parseLong(value));
                }else if (split_list[2].equals("C")) {
                    lineCmap.put(Integer.parseInt(split_list[1]),Long.parseLong(value));
                }
            } else if (split_list[0].equals("2")) {
                //为第二维度
                lineIdTwo.add(split_list[1]);
                number.add(Long.parseLong(value));
            } else if (split_list[0].equals("3")) {
                //为第三维度
                lineIdThree.add(split_list[1]);
                peopleNumber.add(Long.parseLong(value));
            } else if (split_list[0].equals("4")) {
                //为第四维度
                userID.add(split_list[1]);
                numberTime.add(Integer.parseInt(value));
            } else if (split_list[0].equals("5")) {
                //为第五维度
                payType.add(split_list[1]);
                payNumber.add(Long.parseLong(value));
            }
            i++;
        }
        //对第一维度进行调整
        for (int j = 0; j < lineAmap.size(); j++) {
            lineA.add(lineAmap.get(j));
            lineB.add(lineBmap.get(j));
            lineC.add(lineCmap.get(j));
        }
        StationOne stationOne=StationOne.builder()
                .lineA(lineA)
                .lineB(lineB)
                .lineC(lineC)
                .build();

        StationTwo stationTwo=StationTwo.builder()
                .lineId(lineIdTwo)
                .number(number)
                .build();
        StationThree stationThree=StationThree.builder()
                .lineID(lineIdThree)
                .peopleNumber(peopleNumber)
                .build();
        StationFour stationFour=StationFour.builder()
                .userID(userID)
                .numberTime(numberTime)
                .build();
        StationFive stationFive=StationFive.builder()
                .payType(payType)
                .number(payNumber)
                .build();

        StationAll stationAll=StationAll.builder()
                        .stationOne(stationOne)
                                .stationTwo(stationTwo)
                                        .stationThree(stationThree)
                                                .stationFour(stationFour)
                                                        .stationFive(stationFive)
                                                                .build();

        //转化为json字符串
        String station_json = JSONObject.toJSONString(stationAll);

        webSocketServer.sendToAllClient(station_json);
        System.out.println(stationTwo);
        System.out.println(stationThree);
        System.out.println(stationFour);
        System.out.println(stationFive);
    }

}
